﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using QuanLyDaiLy.Model;
using QuanLyDaiLy.Context;
using QuanLyDaiLy.Platform;

namespace QuanLyDaiLy.View
{
    /// <summary>
    /// Interaction logic for WPhieuThu.xaml
    /// </summary>
    public partial class WPhieuThu : Window
    {
        

        private CPhieuThuContext phieuthuContext;
        private CPhieuThu phieuthu; 


        #region Function

        public WPhieuThu()
        {
            InitializeComponent();
            InitializeFeature();
        }

        private void InitializeFeature()
        {
            phieuthuContext = new CPhieuThuContext(CConstant.CONNECTION);
            try
            {
                TaoBangDuLieu();
                LoadItemsCombobox();
                TrangThaiMacDinh();
                CapNhatDuLieuBang();
            }
            catch (Exception e) { MessageBox.Show(e.Message); }
        }

        private void LoadItemsCombobox()
        {
            using (var dailyContext = new CDaiLyContext(CConstant.CONNECTION))
            {
                cbDaiLy.ItemsSource = dailyContext.LayDanhSachTatCa();
                cbDaiLy.SelectedValue = "MaDaiLy";
                cbDaiLy.DisplayMemberPath = "TenDaiLy";
            }
        }

     
        private void TrangThaiMacDinh()
        {
            txtMaPhieuThu.IsEnabled = false;
            cbDaiLy.IsEnabled = false;
            dpNgayThuTien.IsEnabled = false;
            txtSoTienThu.IsEnabled = false;
            btLuu.IsEnabled = false;
            btXoa.IsEnabled = false;

            txtMaPhieuThu.Clear();
            dpNgayThuTien.SelectedDate = DateTime.Now;
            txtSoTienThu.Clear();
            cbDaiLy.SelectedIndex = 0;
        }

        private void TrangThaiThem()
        {
            cbDaiLy.IsEnabled = true;
            dpNgayThuTien.IsEnabled = true;
            txtSoTienThu.IsEnabled = true;
            btLuu.IsEnabled = true;

            txtMaPhieuThu.Text = phieuthuContext.LayMaSoMoi().ToString();
            cbDaiLy.SelectedItem = 0;
            dpNgayThuTien.SelectedDate = DateTime.Now;
            txtSoTienThu.Clear();
        }

        private void TrangThaiChon()
        {
            TrangThaiThem();
            btLuu.IsEnabled = true;
            btXoa.IsEnabled = true;
        }

        private bool KiemTraNhap()
        {
            if (dpNgayThuTien.SelectedDate > DateTime.Now)
            {
                MessageBox.Show("Ngày Thu Tiền Chưa Hợp Lệ");
                return false;
            }
            if (txtSoTienThu.Text == "")
            {
                MessageBox.Show("Vui Lòng Nhập Đầy Đủ Thông Tin");
                txtSoTienThu.Focus();
                return false;
            }
            if (txtSoTienThu.Text.Length > 9)
            {
                MessageBox.Show("Vui Lòng Kiểm Tra Lại Tiền Thu");
                txtSoTienThu.Clear();
                txtSoTienThu.Focus();
                return false;
            }

            return true;
        }
         
        private void LuuPhieuThu()
        {
            try
            {
                if (KiemTraNhap())
                {

                    if (Convert.ToInt32(txtMaPhieuThu.Text) < phieuthuContext.LayMaSoMoi())
                    {
                        var result = MessageBox.Show("Bạn Có Chắc Chắn Sửa Không?", "Thông Báo", MessageBoxButton.YesNo, MessageBoxImage.Question);
                        if (result == MessageBoxResult.Yes)
                        {
                            phieuthu = new CPhieuThu( (cbDaiLy.SelectedValue as CDaiLy).MaDaiLy , Convert.ToInt32(txtSoTienThu.Text), Convert.ToDateTime(dpNgayThuTien.Text));
                            phieuthuContext.CapNhat(phieuthu);
                            MessageBox.Show("Sửa Thành Công");
                        }
                        TrangThaiChon();

                    }
                    else
                    {
                 //       phieuthu = new CPhieuThu((cbDaiLy.SelectedValue as CDaiLy).MaDaiLy,Convert.ToInt32(txtSoTienThu.Text),Convert.ToDateTime(dpNgayThuTien.SelectedDate.Value));
                        phieuthu = new CPhieuThu(4,123123,Convert.ToDateTime("09/02/1994"));
                        phieuthuContext.Them(phieuthu);
                        MessageBox.Show("Lưu Thành Công");
                        TrangThaiThem();
                    }

                    CapNhatDuLieuBang();
                }
            }
            catch (Exception e) { MessageBox.Show(e.Message); }
                
        }

        private void XoaPhieuThu()
        {
            try
            {
                var result = MessageBox.Show("Bạn Có Chắc Chắn Xóa Không?", "Thông Báo", MessageBoxButton.YesNo, MessageBoxImage.Question);
                if (result == MessageBoxResult.Yes)
                {
                    phieuthuContext.Xoa(Convert.ToInt32(txtMaPhieuThu.Text));
                    CapNhatDuLieuBang();
                    TrangThaiMacDinh();  
                    MessageBox.Show("Xóa Thành Công");                 
                }
            }
            catch { MessageBox.Show("Chưa Thể Xóa"); }
        }

        private void ChonPhieuThu()
        {
            try
            {
                TrangThaiChon();
                if (lvPhieuThu.SelectedItem is CPhieuThu)
                {
                    txtMaPhieuThu.Text = (lvPhieuThu.SelectedItem as CPhieuThu).MaPhieuThu.ToString();
                    dpNgayThuTien.SelectedDate = (lvPhieuThu.SelectedItem as CPhieuThu).NgayThuTien;
                    txtSoTienThu.Text = (lvPhieuThu.SelectedItem as CPhieuThu).SoTienThu.ToString();
                    using (var dailyContext = new CDaiLyContext(CConstant.CONNECTION))
                    {
                        int indext = 0;
                        foreach (CDaiLy daily in dailyContext.LayDanhSachTatCa())
                        {
                            if ((lvPhieuThu.SelectedItem as CPhieuThu).MaDaiLy == daily.MaDaiLy)
                            {
                                cbDaiLy.SelectedIndex = indext;
                            }
                            indext++;
                        }
                    }
                }
            }
            catch { }
        }

        private void TaoBangDuLieu()
        {
            try
            {
                GridView grdView = new GridView();
                grdView.AllowsColumnReorder = true;
                grdView.ColumnHeaderToolTip = "PHIEUTHU";

                GridViewColumn maPhieuThu = new GridViewColumn();
                maPhieuThu.DisplayMemberBinding = new Binding("MaPhieuThu");
                maPhieuThu.Header = "Mã Phiếu Thu";
                maPhieuThu.Width = 120;
                grdView.Columns.Add(maPhieuThu);

                GridViewColumn madaily = new GridViewColumn();
                madaily.DisplayMemberBinding = new Binding("MaDaiLy");
                madaily.Header = "Mã Đại Lý";
                madaily.Width = 120;
                grdView.Columns.Add(madaily);
                
                GridViewColumn sothienthu = new GridViewColumn();
                sothienthu.DisplayMemberBinding = new Binding("SoTienThu");
                sothienthu.Header = "Số Tiền Thu";
                sothienthu.Width = 120;
                grdView.Columns.Add(sothienthu);

                GridViewColumn ngaythutien = new GridViewColumn();
                ngaythutien.DisplayMemberBinding = new Binding("NgayThuTien");
                ngaythutien.Header = "Ngày Thu Tiền";
                ngaythutien.Width = 120;
                grdView.Columns.Add(ngaythutien);

                lvPhieuThu.View = grdView;
            }
            catch { }
        }

        private void CapNhatDuLieuBang()
        {
            lvPhieuThu.ItemsSource = phieuthuContext.LayDanhSachTatCa();
        }

        #endregion

        #region EVEN


        private void btPhieuThuMoi_Click(object sender, RoutedEventArgs e)
        {
            TrangThaiThem();
        }

        private void btLuu_Click(object sender, RoutedEventArgs e)
        {
            LuuPhieuThu();
        }

        private void btXoa_Click(object sender, RoutedEventArgs e)
        {
            XoaPhieuThu();
        }

        private void txtSoTienThu_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                LuuPhieuThu();
            }
        }

        private void txtSoTienThu_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (!Char.IsDigit((char)KeyInterop.VirtualKeyFromKey(e.Key)) &&
            !Char.IsControl((char)KeyInterop.VirtualKeyFromKey(e.Key)))
            {
                e.Handled = true;
            }
        }

   
        #endregion


    }
}
